#include "BBox.h"

BBox::BBox()
{
	x = y = w = h = 0;
}

BBox BBox::getIntersection(BBox* other)
{
    if(x >= other->x + other->w || x+w <= other->x || y >= other->y + other->h || y+h <= other->h)
    {
        return BBox();
    }
    else
    {
        BBox r;
        if(x < other->x){
            r.x = other->x;
        }else{
            r.x = x;
        }
		r.w = std::min( x+w, other->x + other->w ) - r.x;
		
		// Calc y
		if(y < other->y){
            r.y = other->y;
        }else{
			r.y = y;
        }
		r.h = std::min( y+h, other->y + other->h ) - r.y;

		if(r.h <= 0 || r.w <= 0){
			return BBox();
		}
        return r;
    }
}

bool BBox::isEmpty(){
	return (w == 0 && h == 0);
}

void BBox::draw()
{
	glRectd(x,y,x+w,y+h);
}
